package com.nchu.commondevelopmentframework.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nchu.commondevelopmentframework.common.domain.ApiResult;
import com.nchu.commondevelopmentframework.common.storage.StorageFactory;
import com.nchu.commondevelopmentframework.dto.UserDTO;
import com.nchu.commondevelopmentframework.entity.User;
import com.nchu.commondevelopmentframework.mapper.UserMapper;
import com.nchu.commondevelopmentframework.query.UserQuery;
import com.nchu.commondevelopmentframework.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.HandlerMapping;

import java.sql.SQLException;
import java.util.List;


/**
 * @className: TestController
 * @description: TODO 类描述
 * @author: Li Chuanwei
 * @date: 2024/01/18 17:00
 * @Company: Copyright [日期] by [作者或个人]
 **/

@RestController
public class TestController {
    @Autowired
    private StorageFactory storageFactory;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @PostMapping ("/post")
    public boolean getUserById(@RequestBody @Validated UserDTO userDTO) {
        System.out.println(storageFactory.get());
        return true;
    }

    @PostMapping("/fileUpload")
    public boolean fileUpload(@RequestParam(value = "file", required = false) MultipartFile file) {
        String s = storageFactory.get().uploadFileAndGetURL(file,"111","222");
//        file.getOriginalFilename()
        System.out.println(s);
        return true;
    }

    @PostMapping("/createDirectory")
    public boolean createDirectory(@RequestParam("directoryName") String directoryName) {
        return storageFactory.get().createDirectory(directoryName);
    }

    @PostMapping("/objectExist")
    public boolean objectExist(@RequestParam("objectName") String objectName) {
        return storageFactory.get().objectExist(objectName);
    }

    @PostMapping("/folderExist")
    public boolean folderExist(@RequestParam("folderName") String folderName) {
        return storageFactory.get().folderExist(folderName);
    }

    @PostMapping("/copyObject")
    public boolean copyObject(){
        return storageFactory.get().copyObject("demo121542452452","222.txt","222/333.txt");
    }

    @GetMapping("/download/**")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response) {
        // 获取完整的请求路径
        String fullPath = (String) request.getAttribute(
                HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);

        // 从路径中去掉"/download/"部分
        String filePath = fullPath.substring("/download/".length());

        // 使用获取到的文件路径进行下载
        storageFactory.get().getObject(filePath, response);
    }

    @GetMapping("/getBucketObjects/**")
    public ApiResult<List<String>> getBucketObjects(HttpServletRequest request, HttpServletResponse response) {
        // 获取完整的请求路径
        String fullPath = (String) request.getAttribute(
                HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);

        // 从路径中去掉"/download/"部分
        String filePath = fullPath.substring("/getBucketObjects/".length());

        // 使用获取到的文件路径进行下载
        return ApiResult.success(storageFactory.get().getBucketObjects(filePath));
    }

    @DeleteMapping("delete/{fileName}")
    public boolean deleteFile(@PathVariable String fileName) {
        return storageFactory.get().removeObject(fileName);
    }

    @PostMapping("getUser")
    public ApiResult<Page<User>> getUser(@RequestBody UserQuery userQuery) throws SQLException {
        return ApiResult.success("666",userService.selectPage(userQuery));
    }
}